﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;

namespace PlaylistParitySL
{
    public class MainVM : BaseVM
    {
        

        /// <summary>
        /// The <see cref="Libraries" /> property's name.
        /// </summary>
        public const string LibrariesPropertyName = "Libraries";

        private ObservableCollection<MediaPlayer> _libraries = new ObservableCollection<MediaPlayer>();

        /// <summary>
        /// Gets the Libraries property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<MediaPlayer> Libraries
        {
            get
            {
                return _libraries;
            }

            set
            {
                if (_libraries == value)
                {
                    return;
                }

                var oldValue = _libraries;
                _libraries = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("Libraries");
            }
        }

        /// <summary>
        /// The <see cref="MyPlaylists" /> property's name.
        /// </summary>
        public const string MyPlaylistsPropertyName = "MyPlaylists";

        private ObservableCollection<Playlist> _myPlaylists = new ObservableCollection<Playlist>();

        /// <summary>
        /// Gets the MyPlaylists property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Playlist> MyPlaylists
        {
            get
            {
                return _myPlaylists;
            }

            set
            {
                if (_myPlaylists == value)
                {
                    return;
                }

                var oldValue = _myPlaylists;
                _myPlaylists = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("MyPlaylists");
            }
        }

        /// <summary>
        /// The <see cref="IsBusy" /> property's name.
        /// </summary>
        public const string IsBusyPropertyName = "IsBusy";

        private bool _isBusy = true;

        /// <summary>
        /// Gets the IsBusy property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }

            set
            {
                if (_isBusy == value)
                {
                    return;
                }

                var oldValue = _isBusy;
                _isBusy = value;

                // Update bindings, no broadcast
                RaisePropertyChanged("IsBusy");
            }
        }
        BackgroundWorker _worker;
        public MainVM()
        {
            IsBusy = true;
            if (DesignerProperties.IsInDesignTool)
            {
                CreateTestPlaylists();

                IsBusy = false;
            }
            else
            {
                _worker = new BackgroundWorker();
                _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                _worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
                _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                _worker.RunWorkerAsync();
            }            
        }

        private void CreateTestPlaylists()
        {
            Libraries.Add(
                new MediaPlayer()
                {
                    Playlists = new ObservableCollection<Playlist>()
                    {
                new Playlist()
                {
                    Name = "Playlist 1",
                    SongList = new ObservableCollection<Song>() 
                        { 
                            new Song() 
                            { 
                                Album = "Test Album", 
                                Artist = "Test Artist", 
                                Location = "location", 
                                Name = "Song Name" 
                            },
                            new Song()
                            {
                                Album = "Album 2",
                                Artist = "Artist 2",
                                Location="Location 2",
                                Name = "Name 2"
                            }
                        }
                }
                    },
                    Name = "Windows Media Player"
                });

            Libraries.Add(
            new MediaPlayer()
            {
                Playlists = new ObservableCollection<Playlist>()
                {
                    new Playlist()
                    {
                        Name = "Playlist 1",
                        SongList = new ObservableCollection<Song>() 
                        { 
                            new Song() 
                            { 
                                Album = "Test Album", 
                                Artist = "Test Artist", 
                                Location = "location", 
                                Name = "Song Name" 
                            },
                            new Song()
                            {
                                Album = "Album 2",
                                Artist = "Artist 2",
                                Location="Location 2",
                                Name = "Name 2"
                            }
                        }
                    },
                    new Playlist()
                    {
                        Name = "Playlist 2",
                        SongList = new ObservableCollection<Song>() 
                        { 
                            new Song() 
                            { 
                                Album = "Test Album", 
                                Artist = "Test Artist", 
                                Location = "location", 
                                Name = "Song Name" 
                            },
                            new Song()
                            {
                                Album = "Album 2",
                                Artist = "Artist 2",
                                Location="Location 2",
                                Name = "Name 2"
                            }
                        }
                    }
                },
                Name = "ITunes"
            });
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Libraries.Add(e.Result as MediaPlayer);
            IsBusy = false;
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Libraries.Add(new MediaPlayer() { Playlists = WMPUtilities.GetPlaylists(), Name = "Windows Media Player" });
            }
            catch { }
            try
            {
                MediaPlayer wmpPlayer = new MediaPlayer() { Playlists = ITunesUtilities.GetPlaylists(), Name = "ITunes" };
                e.Result = wmpPlayer;
            }
            catch { }

            //CreateTestPlaylists();
        }
    }
}
